package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.TradeAreatInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.TradeAreatInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.TradeAreatInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.MchntAreaInfoBean;
import cc.rengu.igas.bomp.facade.request.MchntAreaListQueryRequest;
import cc.rengu.igas.bomp.facade.response.MchntAreaListQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商圈列表信息查询
 * Created by 赵奎伍
 */
public class MchntAreaListQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntAreaListQueryRequest mchntAreaListQuery = new MchntAreaListQueryRequest();
        ConvertUtil.convertOutput(mchntAreaListQuery);
        return mchntAreaListQuery;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntAreaListQueryRequest mchntAreaListQuery = (MchntAreaListQueryRequest) request;
        MchntAreaListQueryResponse mchntAreaListQueryResponse = new MchntAreaListQueryResponse();
        BeanUtil.beanCopy(mchntAreaListQuery, mchntAreaListQueryResponse);

        TradeAreatInfoMapper tradeAreatInfoMapper = new TradeAreatInfoMapperImpl();
        List<TradeAreatInfo> tradeAreatInfoList = new ArrayList<TradeAreatInfo>();

        String instId = mchntAreaListQuery.getHeader().getInstId();
        String mchntArea = mchntAreaListQuery.getMchntArea();
        String mchntAreaName = mchntAreaListQuery.getMchntAreaName();
        String areaCode = mchntAreaListQuery.getAreaCode();
        String signInstId = mchntAreaListQuery.getSignInstId();
        int pageIndex = mchntAreaListQuery.getPageIndex();
        int pageSize = mchntAreaListQuery.getPageSize();
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(pageIndex);
        pageInfo.setPageSize(pageSize);

        if (!StringUtils.isEmpty(mchntArea) || !StringUtils.isEmpty(mchntAreaName) || !StringUtils.isEmpty(areaCode) || !StringUtils.isEmpty(signInstId)) {
            /* 根据商户标识、商圈名称、所属地区代码、所属机构代码查询 */
            if (-1 == pageIndex || -1 == pageSize) {
                tradeAreatInfoList = tradeAreatInfoMapper.selectTradeAreatInfoByKeys(instId, mchntArea, mchntAreaName, areaCode, signInstId);
            } else {
                PageResult pageResult = tradeAreatInfoMapper.selectTradeAreatInfoByKeysByPage(instId, mchntArea, mchntAreaName, areaCode, signInstId, pageInfo);
                if (null != pageResult) {
                    pageInfo = pageResult.getPageInfo();
                    tradeAreatInfoList = pageResult.getResult().stream().map(item -> (TradeAreatInfo) item).collect(Collectors.toList());
                }
            }
        } else {
            /* 根据多法人标识查询所有信息*/
            if (-1 == pageIndex || -1 == pageSize) {
                tradeAreatInfoList = tradeAreatInfoMapper.selectTradeAreatInfoByInstId(instId);
            } else {
                PageResult pageResult = tradeAreatInfoMapper.selectTradeAreatInfoByInstIdByPage(instId, pageInfo);
                if (null != pageResult) {
                    pageInfo = pageResult.getPageInfo();
                    tradeAreatInfoList = pageResult.getResult().stream().map(item -> (TradeAreatInfo) item).collect(Collectors.toList());
                }
            }
        }
        if (null != tradeAreatInfoList && !tradeAreatInfoList.isEmpty()) {
            List<MchntAreaInfoBean> mchntAreaInfoBeans = new ArrayList<>();
            tradeAreatInfoList.forEach(item -> convTradeAreatInfo(item, mchntAreaInfoBeans));
            /* 查询结果赋值 */
            mchntAreaListQueryResponse.setTotalRows(pageInfo.getTotalRows());//总记录数
            mchntAreaListQueryResponse.setTotalPage(pageInfo.getTotalPage());//总页数
            mchntAreaListQueryResponse.setMchntAreaInfoList(mchntAreaInfoBeans);
        } else {
            rglog.error("法人行<{}>还未配置商圈列表信息!", mchntAreaListQuery.getHeader().getInstId());
        }
        /* 按照接口转换基础产品信息并响应给请求方 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntAreaListQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntAreaListQueryResponse mchntAreaListQueryResponse = (MchntAreaListQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntAreaListQueryResponse);
    }

    /**
     * 将数据库表结构对象转换成接口对象
     *
     * @param tradeAreatInfo     数据库表结构对象
     * @param mchntAreaInfoBeans 接口对象
     */
    private void convTradeAreatInfo(TradeAreatInfo tradeAreatInfo, List<MchntAreaInfoBean> mchntAreaInfoBeans) {
        if ("01".equals(tradeAreatInfo.getTradeAreaStatus())) {
            //只展示正式的数据  00-非正式   01-正式   02-停用 03-已注销
            MchntAreaInfoBean tradeAreatInfoBean = new MchntAreaInfoBean();
            BeanUtil.beanCopy(tradeAreatInfo, tradeAreatInfoBean);
            tradeAreatInfoBean.setMchntArea(tradeAreatInfo.getTradeAreaCode());
            tradeAreatInfoBean.setMchntAreaName(tradeAreatInfo.getTradeAreaName());
            tradeAreatInfoBean.setSignInstId(tradeAreatInfo.getBelongInstId());
            tradeAreatInfoBean.setSignInstName(tradeAreatInfo.getBelongInstName());
            tradeAreatInfoBean.setAreaCode(tradeAreatInfo.getBelongAreaCode());
            tradeAreatInfoBean.setAreaName(tradeAreatInfo.getBelongAreaName());
            mchntAreaInfoBeans.add(tradeAreatInfoBean);
        }
    }
}
